package com.siss.tenancy.util;

import javax.servlet.http.HttpServletRequest;

public class ExplorerTypeUtil {

    /**
     * 判断是否是IE浏览器
     *
     * @param userAgent
     * @return
     */
    public static boolean isIE(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        boolean isIe = true;
        int index = userAgent.indexOf("rv:");
        if (index == -1) {
            isIe = false;
        }
        return isIe;
    }
    /**
     * 判断是否是Chrome浏览器
     *
     * @param userAgent
     * @return
     */
    public static boolean isChrome(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        boolean isChrome = true;
        int index = userAgent.indexOf("chrome");
        if (index == -1) {
            isChrome = false;
        }
        return isChrome;
    }
    /**
     * 判断是否是Firefox浏览器
     *
     * @param userAgent
     * @return
     */
    public static boolean isFirefox(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        boolean isFirefox = true;
        int index = userAgent.indexOf("firefox");
        if (index == -1) {
            isFirefox = false;
        }
        return isFirefox;
    }

    public static boolean isSafari(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        boolean isFirefox = true;
        int index = userAgent.indexOf("safari");
        if (index == -1) {
            isFirefox = false;
        }
        return isFirefox;
    }
    /**
     * 获取客户端类型
     *
     * @param userAgent
     * @return
     */
    public static String getClientExplorerType(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        String explorer = "other";
        if (isIE(request)) {
            int index = userAgent.indexOf("rv:");
            explorer = "ie";
        } else if (isChrome(request)) {
            int index = userAgent.indexOf("chrome");
            explorer = "chrome";
        } else if (isFirefox(request)) {
            int index = userAgent.indexOf("firefox");
            explorer = "firefox";
        }else if (isSafari(request)) {
            int index = userAgent.indexOf("safari");
            explorer = "safari";
        }
        return explorer.toUpperCase();
    }

}
